#include <iostream>
#include <unistd.h>
#include <pthread.h>

using namespace std;

int g_val = 0;

void *threadHandler1(void *args)
{
    while (true)
    {
        printf("我是次线程1，我正在运行... &g_val: %p  g_val: %d\n", &g_val, g_val);
        sleep(1);
    }
}

void *threadHandler2(void *args)
{
    while (true)
    {
        printf("我是次线程2，我正在运行... &g_val: %p  g_val: %d\n", &g_val, g_val);
        g_val++; // 次线程2 每次都需改这个全局变量
        sleep(1);
    }
}

int main()
{
    pthread_t t1, t2; // 创建两个线程
    pthread_create(&t1, NULL, threadHandler1, NULL);
    pthread_create(&t2, NULL, threadHandler2, NULL);

    // 主线程运行
    while (true)
    {
        printf("我是主线程，我正在运行... &g_val: %p  g_val: %d\n", &g_val, g_val);
        sleep(1);
    }

    return 0;
}
